package br.com.cjweb.util.lang;

import java.util.StringTokenizer;

public final class StringUtil
{
	public static String getStringEspacoExcedente(String stringEntrada)
	{
		StringBuilder sb = null;
		StringTokenizer token = null;
		try
		{
			if (stringEntrada != null)
			{
				sb = new StringBuilder();
				token = new StringTokenizer(stringEntrada);
				while (token.hasMoreTokens())
				{
					sb.append(token.nextToken() + " ");
				}
				return sb.toString().trim();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
			
			if (token != null)
				token = null;
		}
		return "";
	}

	public static String getStringformatada(String stringEntrada, String mascara)
	{
		StringBuffer sb = null;
		try
		{
			sb = new StringBuffer();
			int posicaoNumero = 0;
			for (int posicao = 0; posicao < mascara.length(); posicao++)
			{
				if (String.valueOf(mascara.charAt(posicao)).trim().equals("#"))
				{
					sb.append(String.valueOf(stringEntrada.charAt(posicaoNumero)));
					posicaoNumero++;
				}
				else
				{
					sb.append(String.valueOf(mascara.charAt(posicao)));
				}
			}
			return sb.toString();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
		}
		return "";
	}

	public static String getSomenteNumeros(String stringEntrada)
	{
		StringBuffer sb = null;
		try
		{
			sb = new StringBuffer();
			for (int i = 0; i < stringEntrada.length(); i++)
			{
				if (Character.isDigit(stringEntrada.charAt(i)))
				{
					sb.append(String.valueOf(stringEntrada.charAt(i)));
				}
			}
			return sb.toString();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
		}
		return "";
	}
}
